﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using System.Web.Security;
using DSAAssignment.Models;

namespace DSAAssignment.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult About()
        {
            return View();
        }

        public ActionResult HomePage()
        {
            List<Product> list = new ProductServ.ProductServicesClient().GetAllProducts().ToList();
            return View("HomePage", list);
        }

        public ActionResult MyPurchaseProducts()
        {
            List<PurchaseModel> list = new List<PurchaseModel>();
            foreach (Common.VIews.UserItem u in new ProductServ.ProductServicesClient().GetUserProducts(HttpContext.User.Identity.Name))
            {
                PurchaseModel p = new PurchaseModel();
                p.PurchaseId = u.PurchaseID;
                p.ProductID = u.ID;
                p.ProductName = u.ProductName;
                UserProductFault f = new FaultServ.FaultServiceClient().getFaultByID(u.FaultID);
                if (f == null || f.FaultID == 7)
                    p.Fault = "No Faults";
                else
                    p.Fault = f.Fault.Fault1;

                p.FaultID = (int)f.FaultID;
                p.Price = u.AmountPurchased;
                p.WarrantyDate = u.WarranyExpires;
                list.Add(p);
            }

            return View("MyPurchaseProducts", list);
        }

        public ActionResult MyOrders()
        {
            return View("MyOrders", new ProductServ.ProductServicesClient().GetAllOrderByUserName(HttpContext.User.Identity.Name));
        }

        public ActionResult Category(int id)
        {
            List<Product> list = new ProductServ.ProductServicesClient().Search(id,"",0,0).ToList();
            return View("HomePage", list);
        }

        public ActionResult SortBy(string sorting)
        {
            List<Common.Product> list = new ProductServ.ProductServicesClient().GetAllProducts().OrderByDescending(x => x.Name).ToList();

            if (sorting.Equals("Highest Price"))
            {
                list.OrderBy(p => p.Price).ToList();
            }
            else
            {
                list.OrderByDescending(p => p.Price).ToList();
            }

            return View("HomePage", list);
        }

        public ActionResult PriceRange(string ValueSort)
        {
            List<Product> list = new ProductServ.ProductServicesClient().GetAllProducts().ToList();
            List<Product> sorted = new List<Product>();

            if (ValueSort.Equals("PriceRange1"))
            {
                foreach (Product p in list)
                {
                    if (p.Price > 100 && p.Price < 200)
                    {
                        sorted.Add(p);
                    }
                }
            }
            else if (ValueSort.Equals("PriceRange2"))
            {
                foreach (Product p in list)
                {
                    if (p.Price > 200 && p.Price < 400)
                    {
                        sorted.Add(p);
                    }
                }
            }
            else
            {
                foreach (Product p in list)
                {
                    if (p.Price > 400)
                    {
                        sorted.Add(p);
                    }
                }

            }
            return View("HomePage", sorted.OrderBy(x => x.Price).ToList());
        }

        public ActionResult SearchBy(string ProductName)
        {
            List<Product> list = new ProductServ.ProductServicesClient().Search(0,ProductName,0,0).ToList();
            return View("HomePage", list);
        }

        public ActionResult ReportsView()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult ManageFault()
        {
            return View("ManageFaults");
        }
    }
}
